package com.jketing.bean;

import java.util.List;

import com.jketing.Config;

/**
 * PageBean 翻页数据封装辅助类
 * 
 * @author  李荣华
 * @version v1.0
 * @date     2013-04-25
 * @since   JDK1.6
 */
public class PageBean<T> {
	/**
	 *   int totalPage  页总数	
	 */
	private int totalPage;
	/**
	 *   int currentPage  当前页码
	 */
	private int currentPage;
	/**
	 *   int pageSize  每页大小
	 */
	private int pageSize;
	/**
	 *   long resultSize  结果集记录总数
	 */
	private long resultSize;
	/**
	 *   int start  开始位置
	 */
	private int start;
	/**
	 *   int end   结束位置	
	 */
	private int end;
	/**
	 *   List<T> content 数据内容列表	
	 */
	private List<T> content;
	
	/**
	 *   PageBean 构造方法
	 *   
	 *   @param resultSize 总大小	
	 *   @param pageCount 页大小
	 */
	public PageBean(long resultSize,int currentPage) {
		this.currentPage = currentPage;		
		this.resultSize = resultSize;
		pageSize = Config.getPageSize();
		setTotalPage();
		setStart();
		setEnd();
	}
	
	/**
	 *   PageBean 构造方法
	 *   
	 *   @param resultSize 总大小	
	 *   @param currentPage 当前页
	 *   @param pageSize 页大小
	 */
	public PageBean(long resultSize,int currentPage, int pageSize) {
		this.currentPage = currentPage;
		this.pageSize = pageSize;
		this.resultSize = resultSize;
		setTotalPage();
		setStart();
		setEnd();
	}
	
	/**
	 *   int totalPage  页总数	
	 */
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage() {
		long pageMod = resultSize % pageSize;
		if (pageMod == 0){
			totalPage = (int)(resultSize / pageSize);
		}
		else{
			totalPage = (int)((resultSize - pageMod) / pageSize + 1);
		}
	}
	
	/**
	 *   int currentPage  当前页码
	 */
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	
	/**
	 *   int pageSize  每页大小
	 */
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	/**
	 *   long resultSize  结果集记录总数
	 */
	public long getResultSize() {
		return resultSize;
	}
	public void setResultSize(long resultSize) {
		this.resultSize = resultSize;
	}
	
	/**
	 *   int start  开始位置
	 */
	public int getStart() {
		return start;
	}
	public void setStart() {
		if (currentPage < totalPage) {
			start = (currentPage - 1) * pageSize;
		} else {
			start = (totalPage - 1) * pageSize;
		}
	}
	
	/**
	 *   int end   结束位置	
	 */
	public int getEnd() {
		return end;
	}
	public void setEnd() {
		if (currentPage < totalPage) {
			end = (currentPage + 1) * pageSize - 1;
		} else {
			end = totalPage * pageSize - 1;
		}
	}
	
	/**
	 *   List<T> content 数据内容列表	
	 */
	public List<T> getContent() {
		return content;
	}
	public void setContent(List<T> content) {
		this.content = content;
	}
	
	/**
	 *   hasNext 是否有下一页
	 *   
	 *   
	 *   @return true 有下一页
	 */
	public boolean hasNext(){
		boolean retVal = false;
		if ((currentPage + 1) <= totalPage) {
			retVal = true;
		}
		return retVal;
	}
	
	/**
	 *   hasUp 是否有上一页
	 *   
	 *   @return true 有上一页
	 */
	public boolean hasUp(){
		boolean retVal = false;
		if ((currentPage - 1) >= 1) {
			retVal = true;
		}
		return retVal;
	}
}


